﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Linq;

namespace MoreEPG.Model
{
    public class ConfigChannelCollections
    {
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private string XmlFile = HelperClass.confPath + @"Sender.xml";
        Core _Core = Core.Instance;

        private ObservableCollection<ConfigChannelItem> _Items;
        public ObservableCollection<ConfigChannelItem> Items
        {
            get
            {
                if (_Items == null)
                {
                    this.FillItems();
                }
                return _Items;
            }
            set
            {
                if (_Items == value)
                {
                    return;
                }

                _Items = value;
            }
        }

        private void FillItems()
        {
            
            if (File.Exists(XmlFile))
            {
                XDocument XDoc = XDocument.Load(XmlFile);
                var lst = from item in XDoc.Descendants("Items")
                          select new ConfigChannelItem
                          {
                              Active = (bool)item.Attribute("Active"),
                              Channel = (string)item.Attribute("Channel"),
                              URL = (string)item.Attribute("URL"),
                              Service = (string)item.Attribute("Service"),
                              MatchName = (string)item.Attribute("MatchName"),
                              isHDTV = (bool)item.Attribute("isHDTV")
                          };
                _Items = new ObservableCollection<ConfigChannelItem>(lst);
            }
        }


        public void Save()
        {
            if (this.Items == null)
            {
                logger.Error("Collection is null");
                throw new ApplicationException("ConfigChannelItemCollection is null!");
            }
            XDocument xDocument = new XDocument();
            xDocument.Add(new XElement("Items"));
            foreach (ConfigChannelItem item in this.Items)
            {
                XElement xElement = CreateXmlElement(item);
                xDocument.Element("Items").Add(xElement);
            }

            xDocument.Save(XmlFile);
        }

        private static XElement CreateXmlElement(ConfigChannelItem item)
        {
            XElement xElement = new XElement(
                "ConfigChannelItem",
                new XElement("Active", item.Active),
                new XElement("Channel", item.Channel),
                new XElement("URL", item.URL),
                new XElement("Service", item.Service),
                new XElement("MatchName", item.MatchName),
                 new XElement("isHDTV", item.isHDTV)
            );
            
            return xElement;
        }
        


    }
}
